package com.wiloon.java8x.basic;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

/*
 * Created by roy on 9/27/17;
 */
public class TimerX {
    public static void main(String[] args) {
        Timer retryTimer = new Timer("timer-cancel");
        retryTimer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        System.out.println("***");
                    }
                },
                TimeUnit.SECONDS.toMillis(1),
                TimeUnit.SECONDS.toMillis(1)
        );

        try {
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        retryTimer.cancel();

        retryTimer = new Timer("timer-new");
        retryTimer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        System.out.println("&&&");
                    }
                },
                TimeUnit.SECONDS.toMillis(1),
                TimeUnit.SECONDS.toMillis(1)
        );

        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
